package com.shiro.work1.service;

import com.shiro.work1.dao.TeacherDao;
import com.shiro.work1.domain.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private RedisTemplate redisTemplate;//用来操作redie,springDate核心对象


    public List<Teacher> findAll() {
        //1.查询redis 数据
        //2.判断数据是否存在
        boolean isExists = redisTemplate.hasKey("teacher");
        //3.如果不存在，查询数据库，然后存储到redis
        if(!isExists){
            List<Teacher> teacherList = teacherDao.findAll();
            teacherList.forEach(c->{
                redisTemplate.opsForHash().put("teacher",c.getId()+"",c.getName());
            });
            System.out.println("数据库返回");
            return teacherList;
        }
        //4.如果存在，直接返回缓存的数据
        else{
            Map<String,String> teachers = redisTemplate.opsForHash().entries("teacher");
            List<Teacher> teachersList= new ArrayList<>();
            Set<Map.Entry<String, String>> entries = teachers.entrySet();
            entries.forEach(c->{
                Teacher teacher = new Teacher();
                teacher.setId(Integer.parseInt(c.getKey()));
                teacher.setName(c.getValue());
                teachersList.add(teacher);
            });
            System.out.println("缓存返回");
            return teachersList;
        }
    }

  
    public Teacher findById(int id) {
        //1.查询redis 数据
        //2.判断数据是否存在
        //3.id处理
        String tid="t"+id;
        boolean isExists = redisTemplate.hasKey(tid);
        //4.如果不存在，查询数据库，然后存储到redis
        if(!isExists){
            Teacher teacher = teacherDao.findById(id);
            redisTemplate.opsForValue().set(tid,teacher.getName());
            System.out.println("数据库返回");
            return teacher;
        }
        //5.如果存在，直接返回缓存的数据
        else{
            String name = (String) redisTemplate.opsForValue().get(tid);
            Teacher teacher = new Teacher();
            teacher.setId(id);
            teacher.setName(name);
            System.out.println("缓存返回");
            return teacher;
        }
    }

    
    public List<Teacher> findByName(String name) {
        //1.查询redis 数据
        //2.判断数据是否存在
        boolean isExists = redisTemplate.hasKey(name);
        //3.如果不存在，查询数据库，然后存储到redis
        if(!isExists){
            List<Teacher> teacherList = teacherDao.findByName(name);
            teacherList.forEach(c->{
                redisTemplate.opsForList().leftPush(name,c.getId());
            });
            System.out.println("数据库返回");
            return teacherList;
        }
        //4.如果存在，直接返回缓存的数据
        else{
            List<Integer> ids = redisTemplate.opsForList().range(name, 0, -1);
            List<Teacher> teachersList = new ArrayList<>();
            ids.forEach(id->{
                Teacher teacher = new Teacher();
                teacher.setId(id);
                teacher.setName(name);
                teachersList.add(teacher);
            });
            System.out.println("缓存返回");
            return teachersList;
        }
    }

    public void save(Teacher teacher) {
        //1.增删改数据库
        //2.清空redis缓存
        teacherDao.save(teacher);
        Set keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }

    public void update(Teacher teacher) {
        teacherDao.update(teacher);
        Set keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }

    
    public void delete(int id) {
        teacherDao.delete(id);
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }
}
